import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginHost from '@/components/LoginHost.vue'
import HomeHost from '@/components/HomeHost.vue'
import WecomWell from '@/components/WecomWell'
import User from '@/components/User/UserLogin'
Vue.use(VueRouter)

const routes = [{
  path: '/login',
  component: LoginHost
},
{
  path: '/',
  redirect: '/login'
},
{
  path: '/home',
  component: HomeHost,
  redirect: '/Wecomwell',
  //  子路由配置
  children: [
    {
      path: '/Wecomwell',
      component: WecomWell
    },
    {
      path: '/users',
      component: User
    }
  ]
}
]

const router = new VueRouter({
  routes
})
//  挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  //  获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
// to 将要访问的路径
//  from 代表从哪个路径跳转过来
//  next 是一个函数
export default router
